package ProxyController;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import java.io.*;

public class ProxyFinder {

	public WebDriver driver;
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("answering weegy");

		new ProxyFinder();
		
	}
	
	public ProxyFinder() {
		ArrayList<String> proxies = this.getProxiesHideMy();
		this.addProxies(proxies);
		System.out.println(proxies.size());

	}

	public void addProxies(ArrayList<String> proxies) {
		Connection conn = null;
  
		try {
			String userName = "root";
			String password = "";
			String url = "jdbc:mysql://localhost/proxy_db";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, userName, password);
			System.out.println("Database connection established USER EXISTS");
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
			System.out.println(e);
		} finally {
			if (conn != null) {
				try {

					Statement s = conn.createStatement();
					for(String eachProxy : proxies) {
						String proxyParts[] = eachProxy.split(":");
						String query = "INSERT INTO `proxy_db`.`proxies` (`address`, `port`, `type`, `country`) VALUES ('" + proxyParts[0] + "','" + proxyParts[1] + "','" + proxyParts[2] + "','" + proxyParts[3] + "')";
						System.out.println(query);
						try {
							s.executeUpdate(query);
						} catch (Exception e) {
						
						}
					}

				} catch (Exception e) { /* ignore close errors */
					System.out.println(e);
				}
			}
		}
	System.out.println("done");
	}
	
	public void configureDriver() {
		//File filepath = new File("/home/liam/.mozilla/firefox/e15e9mu2.default");
		//FirefoxProfile profile = new FirefoxProfile(filepath);
		driver = new HtmlUnitDriver();
	}

	public ArrayList<String> getProxiesHideMy() {
		this.configureDriver();
		ArrayList<String> masterIPList = new ArrayList<String>();
		ArrayList<String> tempIPList;
		driver.get("http://hidemyass.com/proxy-list/search-243420/");
		List<WebElement> pageNumbersRaw = driver.findElements(By.tagName("li"));
		String totalPages = "";
		for(int i=0; i<pageNumbersRaw.size(); i++) {
			System.out.println("EACH PAGE:" + pageNumbersRaw.get(i).getText());
			if(pageNumbersRaw.get(i).getText().equals("Next »")) {
				totalPages = pageNumbersRaw.get(i-1).getText();
			}
		}
		driver.close();
		Integer pageCount = Integer.parseInt(totalPages);
		//pageCount = 1;
		for(int i=0; i<pageCount; i++) {
			tempIPList = this.getProxiesHideMyPage(i);
			//for(String eachIP : tempIPList) {
				masterIPList.addAll(tempIPList);
			//}
		}
		return masterIPList;
	}
	
	public ArrayList<String> getProxiesHideMyPage(int page) {
		this.configureDriver();
		driver.get("http://hidemyass.com/proxy-list/search-243420/" + page);
		List<WebElement> ipsRaw = driver.findElements(By.tagName("tr"));
		ArrayList<String> ipsFiltered = new ArrayList<String>();
		
		for(WebElement eachIPRaw : ipsRaw) {
			String eachIPType = "";
			String eachIPString = eachIPRaw.getText();
			String eachPortString = "";
			String eachIPCountry = "";
			
			if(eachIPString.contains("Brazil")) {
				eachIPCountry = "brazil";
			} else if(eachIPString.contains("South Africa")) {
				eachIPCountry = "southafrica";
			} else if(eachIPString.contains("India")) {
				eachIPCountry = "india";
			} else if(eachIPString.contains("China")) {
				eachIPCountry = "china";
			} else if(eachIPString.contains("Indonesia")) {
				eachIPCountry = "indonesia";
			} else if(eachIPString.contains("United States")) {
				eachIPCountry = "unitedstates";
			} else if(eachIPString.contains("Russian Federation")) {
				eachIPCountry = "russianfederation";
			} else if(eachIPString.contains("Colombia")) {
				eachIPCountry = "colombia";
			} else if(eachIPString.contains("Iran")) {
				eachIPCountry = "iran";
			} else if(eachIPString.contains("Tailand")) {
				eachIPCountry = "tailand";
			} else if(eachIPString.contains("Argentina")) {
				eachIPCountry = "argentina";
			} else if(eachIPString.contains("Turkey")) {
				eachIPCountry = "turkey";
			} else if(eachIPString.contains("Egypt")) {
				eachIPCountry = "egypt";
			} else if(eachIPString.contains("Chile")) {
				eachIPCountry = "chile";
			} else if(eachIPString.contains("Taiwan, Republic of China")) {
				eachIPCountry = "taiwan";
			} else if(eachIPString.contains("Nigeria")) {
				eachIPCountry = "nigeria";
			} else if(eachIPString.contains("Ukraine")) {
				eachIPCountry = "ukraine";
			} else if(eachIPString.contains("Korea, Republic of")) {
				eachIPCountry = "korea";
			} else if(eachIPString.contains("Venezuela")) {
				eachIPCountry = "venezuela";
			} else if(eachIPString.contains("Germany")) {
				eachIPCountry = "germany";
			} else if(eachIPString.contains("Ecuador")) {
				eachIPCountry = "ecuador";
			} else if(eachIPString.contains("Peru")) {
				eachIPCountry = "peru";
			} else if(eachIPString.contains("Czech Republic")) {
				eachIPCountry = "czech";
			} else if(eachIPString.contains("South Africa")) {
				eachIPCountry = "southafrica";
			} else if(eachIPString.contains("Hong Kong")) {
				eachIPCountry = "hongkong";
			} else if(eachIPString.contains("Poland")) {
				eachIPCountry = "poland";
			} else if(eachIPString.contains("Japan")) {
				eachIPCountry = "japan";
			} else if(eachIPString.contains("Malaysia")) {
				eachIPCountry = "malaysia";
			} else if(eachIPString.contains("Sweden")) {
				eachIPCountry = "sweden";
			} else if(eachIPString.contains("Austria")) {
				eachIPCountry = "austria";
			} else if(eachIPString.contains("Moldova")) {
				eachIPCountry = "moldova";
			} else if(eachIPString.contains("Canada")) {
				eachIPCountry = "canada";
			} else if(eachIPString.contains("Mexico")) {
				eachIPCountry = "mexico";
			} else if(eachIPString.contains("Romania")) {
				eachIPCountry = "romania";
			} else if(eachIPString.contains("Hungary")) {
				eachIPCountry = "hungary";
			} else if(eachIPString.contains("Italy")) {
				eachIPCountry = "italy";
			} else if(eachIPString.contains("Kenya")) {
				eachIPCountry = "kenya";
			} else if(eachIPString.contains("Philippines")) {
				eachIPCountry = "philippines";
			} else if(eachIPString.contains("Denmark")) {
				eachIPCountry = "denmark";
			} else if(eachIPString.contains("Bangladesh")) {
				eachIPCountry = "bangladesh";
			} else if(eachIPString.contains("Palestinian")) {
				eachIPCountry = "palestine";
			} else if(eachIPString.contains("United Arab Emirates")) {
				eachIPCountry = "unitedarabemirates";
			}  else if(eachIPString.contains("United Kingdom")) {
				eachIPCountry = "unitedkingdom";
			} else if(eachIPString.contains("Lebanon")) {
				eachIPCountry = "lebanon";
			} else if(eachIPString.contains("Iraq")) {
				eachIPCountry = "iraq";
			} else if(eachIPString.contains("Cambodia")) {
				eachIPCountry = "cambodia";
			} else if(eachIPString.contains("Macedonia")) {
				eachIPCountry = "macedonia";
			} else if(eachIPString.contains("Paraguay")) {
				eachIPCountry = "paraguay";
			} else if(eachIPString.contains("Spain")) {
				eachIPCountry = "spain";
			} else if(eachIPString.contains("Slovakia")) {
				eachIPCountry = "slovakia";
			} else if(eachIPString.contains("Greece")) {
				eachIPCountry = "greece";
			} else if(eachIPString.contains("Honduraas")) {
				eachIPCountry = "honduras";
			} else if(eachIPString.contains("Serbia")) {
				eachIPCountry = "serbia";
			} else if(eachIPString.contains("Pakistan")) {
				eachIPCountry = "pakistan";
			} else if(eachIPString.contains("Azerbaijan")) {
				eachIPCountry = "azerbaijan";
			} else if(eachIPString.contains("Ghana")) {
				eachIPCountry = "ghana";
			} else if(eachIPString.contains("Europe")) {
				eachIPCountry = "europe";
			} else if(eachIPString.contains("France")) {
				eachIPCountry = "france";
			} else if(eachIPString.contains("Puerto Rico")) {
				eachIPCountry = "puertorico";
			} else if(eachIPString.contains("Viet Nam")) {
				eachIPCountry = "vietnam";
			} else if(eachIPString.contains("Albania")) {
				eachIPCountry = "albania";
			} else if(eachIPString.contains("Bulgaria")) {
				eachIPCountry = "bulgaria";
			} else if(eachIPString.contains("Afghanistan")) {
				eachIPCountry = "afghanistan";
			} else if(eachIPString.contains("Armenia")) {
				eachIPCountry = "armenia";
			} else if(eachIPString.contains("Mongolia")) {
				eachIPCountry = "mongolia";
			} else if(eachIPString.contains("Malta")) {
				eachIPCountry = "malta";
			} else if(eachIPString.contains("Switzerland")) {
				eachIPCountry = "switzerland";
			} else if(eachIPString.contains("Nepal")) {
				eachIPCountry = "nepal";
			} else if(eachIPString.contains("Norway")) {
				eachIPCountry = "norway";
			} else if(eachIPString.contains("Slovenia")) {
				eachIPCountry = "slovenia";
			} else if(eachIPString.contains("Netherlands")) {
				eachIPCountry = "netherlands";
			} else if(eachIPString.contains("Latvia")) {
				eachIPCountry = "latvia";
			} else if(eachIPString.contains("Singapore")) {
				eachIPCountry = "singapore";
			} else if(eachIPString.contains("Namibia")) {
				eachIPCountry = "namibia";
			} else if(eachIPString.contains("Algeria")) {
				eachIPCountry = "algeria";
			} else if(eachIPString.contains("Zambia")) {
				eachIPCountry = "zambia";
			} else if(eachIPString.contains("Seychelles")) {
				eachIPCountry = "seychelles";
			} else if(eachIPString.contains("Jordan")) {
				eachIPCountry = "jordan";
			} else if(eachIPString.contains("Uganda")) {
				eachIPCountry = "uganda";
			} else if(eachIPString.contains("Australia")) {
				eachIPCountry = "australia";
			} else if(eachIPString.contains("Sudan")) {
				eachIPCountry = "sudan";
			} else if(eachIPString.contains("Jamaica")) {
				eachIPCountry = "jamaica";
			} else if(eachIPString.contains("Saudi Arabia")) {
				eachIPCountry = "saudiarabia";
			} else if(eachIPString.contains("Kuwait")) {
				eachIPCountry = "kuwait";
			} else if(eachIPString.contains("Brunei")) {
				eachIPCountry = "brunei";
			} else if(eachIPString.contains("Estonia")) {
				eachIPCountry = "estonia";
			} else if(eachIPString.contains("Fiji")) {
				eachIPCountry = "fiji";
			} else if(eachIPString.contains("Chad")) {
				eachIPCountry = "chad";
			} else if(eachIPString.contains("Angola")) {
				eachIPCountry = "angola";
			} else if(eachIPString.contains("Bosnia")) {
				eachIPCountry = "bosnia";
			} else if(eachIPString.contains("Benin")) {
				eachIPCountry = "benin";
			} else if(eachIPString.contains("Cote")) {
				eachIPCountry = "cote";
			} else if(eachIPString.contains("Dominican Republic")) {
				eachIPCountry = "dominicanrepublic";
			} else if(eachIPString.contains("Georgia")) {
				eachIPCountry = "georgia";
			} else {
				eachIPCountry = "na";
			}
			
			if(eachIPString.contains("HTTPS")) {
				eachIPType = "HTTPS";
			} else if(eachIPString.contains("HTTP")) {
				eachIPType = "HTTP";
			} else {
				eachIPType = "na";
			}
			
			if(eachIPString.contains("and")) {
				eachIPString = eachIPString.substring(eachIPString.indexOf("and")+4);
			}
			if(eachIPString.contains(" ")) {
				eachIPString = eachIPString.substring(eachIPString.indexOf(" "), eachIPString.lastIndexOf(" "));
			}

			eachIPString = eachIPString.replaceAll("[^.01234567890 ]","");
			
			while(eachIPString.startsWith(" ")) {
				eachIPString = eachIPString.substring(1);
			}
			while(eachIPString.endsWith(" ")) {
				eachIPString = eachIPString.substring(0,eachIPString.length()-1);
			}
			
			while(eachIPString.contains(" ")) {
			eachPortString = eachIPString.substring(eachIPString.lastIndexOf(" ")+1,eachIPString.length());
			eachIPString = eachIPString.substring(0, eachIPString.lastIndexOf(" "));
			}
			
			System.out.println(eachIPString + ":" + eachPortString + ":" + eachIPType + ":" + eachIPCountry);
			if(!eachIPType.equals("na") && !eachIPString.equals("") && !eachPortString.equals("")) {
			ipsFiltered.add(eachIPString + ":" + eachPortString + ":" + eachIPType + ":" + eachIPCountry);
			}
		}
		driver.close();
		return ipsFiltered;
	}
	
}
